package combination;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-07-05 09:51
 * @version: 1.0
 */
public class ThreeSum {
    List<String> list = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) return list;
        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");

        dfs(digits, map, 0, new StringBuilder());
        return list;
    }

    private void dfs(String digits, Map<Character, String> map, int index, StringBuilder sb) {
        if (sb.length() == digits.length()) {
            list.add(sb.toString());
        } else {
            char c = digits.charAt(index);
            String s = map.get(c);
            for (int i = 0; i < s.length(); i++) {
                sb.append(s.charAt(i));
                dfs(digits, map, index + 1, sb);
                sb.deleteCharAt(sb.length() - 1);
            }
        }
    }
}
